import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/ai/ai_combo_entity.dart';
import 'package:sports_new/entity/ai/ai_item_entity.dart';
import 'package:sports_new/entity/match/zq_model_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/ai_api.dart';
import 'package:sports_new/view/pick/ai/ai_type.dart';
import 'package:sports_new/view/pick/items/ai_combo_item.dart';
import 'package:sports_new/view/pick/items/ai_match_item.dart';
import 'package:sports_new/view/pick/items/ai_plan_item.dart';

import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/ads_label.dart';
import '../../pick/items/ai_lm_item.dart';

class ZqModelView extends StatefulWidget {
  const ZqModelView({super.key, required this.matchId});

  final int matchId;

  @override
  State<ZqModelView> createState() => _ZqModelViewState();
}

class _ZqModelViewState extends State<ZqModelView> {
  List<ZqModelEntity>? data;

  @override
  void initState() {
    Utils.onEvent('bs_mx');
    getData();
    super.initState();
  }

  Future getData() async {
    var result = await AiApi.getMatchModel(widget.matchId);
    setState(() {
      data = result;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MediaQuery.removePadding(
      context: context,
      removeTop: true,
      child: ListView(
          children:
              (data?.map((e) => modelItem(e)).toList() ?? []).insertWith(0, [
        const AdsLabel(
          adsTag: "match_zq_ai",
          tapType: 'ggw_bsxq_ai',
          margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
        ),
      ]).append(Container(height: 60))),
    );
  }

  Widget modelItem(ZqModelEntity entity) {
    AiType? aiType = AiType.getTypeByPayId(entity.type ?? 0);
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          Utils.onEvent('match_detail_model_more');
          Get.toNamed(aiType?.route ?? "");
        },
        child: Row(
          children: [
            QkdText(aiType?.name ?? "",
                fontSize: 16, fontWeight: FontWeight.w500),
            const Spacer(),
            const QkdText("更多", color: Colours.grey66),
            Container(width: 2),
            Image.asset(Utils.getImgPath("arrow_right_48.png"),
                width: 16, color: Colours.grey66)
          ],
        ).paddingOnly(bottom: 12, top: 12),
      ),
      if (aiType == AiType.combo) ...[
        AiComboItem(
          margin: EdgeInsets.zero,
          item: DetailItems.fromJson(entity.view ?? {}),
          onChange: (change) => getData(),
          onTap: () => Utils.onEvent('match_detail_model_open'),
        )
      ] else if (aiType == AiType.plan) ...[
        AiPlanItem(
          margin: EdgeInsets.zero,
          item: AiItemEntity.fromJson(entity.view ?? {}),
          onChange: (change) => getData(),
          onTap: () => Utils.onEvent('match_detail_model_open'),
        )
      ] else if (aiType == AiType.lm) ...[
        AiLmItem(
          margin: EdgeInsets.zero,
          item: AiItemEntity.fromJson(entity.view ?? {}),
          onChange: (change) => getData(),
          onTap: () => Utils.onEvent('match_detail_model_open'),
        )
      ] else ...[
        AiMatchItem(
          margin: EdgeInsets.zero,
          item: AiItemEntity.fromJson(entity.view ?? {}),
          type: aiType,
          onChange: (change) => getData(),
          onTap: () => Utils.onEvent('match_detail_model_open'),
        )
      ]
    ]).paddingSymmetric(horizontal: 12);
  }
}
